using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Glpt.Data
{
    /// <summary>
    /// 参数类型类别
    /// </summary>
    public enum DbParamType
    {
        /// <summary>
        /// 字符
        /// </summary>
        VarChar = 0,
        /// <summary>
        /// 整型
        /// </summary>
        Int = 1,
        /// <summary>
        /// 浮点型
        /// </summary>
        Double = 2,
        /// <summary>
        /// 时间型
        /// </summary>
        DateTime = 3,
        /// <summary>
        /// BLOB
        /// </summary>
        Blob = 4,
        /// <summary>
        /// NVarChar
        /// </summary>
        NVarChar = 5,
        /// <summary>
        /// Char
        /// </summary>
        Char = 6
    }

    /// <summary>
    /// 命令参数类
    /// </summary>
    [System.Xml.Serialization.XmlInclude(typeof(System.DBNull))]
    public class DbParam
    {
        /// <summary>
        /// 参数编号
        /// </summary>
        public int ParameterNo = 0;
        /// <summary>
        /// 参数名
        /// </summary>
        public string ParameterName = "";
        /// <summary>
        /// 参数值
        /// </summary>
        public object Value = DBNull.Value;
        /// <summary>
        /// 参数类型
        /// </summary>
        public DbParamType DataType = DbParamType.VarChar;
        /// <summary>
        /// 参数方向
        /// </summary>
        public ParameterDirection Direction = ParameterDirection.Input;
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public DbParam()
        {
        }

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="param_name">参数名</param>
        /// <param name="paramValueType">参数类型</param>
        public DbParam(string param_name, DbParamType paramValueType)
        {
            Init(param_name, null, paramValueType, false);
        }

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="param_name">参数名</param>
        /// <param name="param_value">参数值</param>
        /// <param name="paramValueType">参数类型</param>
        /// <param name="paramOutType">是否是输出类型</param>
        public DbParam(string param_name, object param_value, DbParamType paramValueType, bool paramOutType)
        {
            Init(param_name, param_value, paramValueType, paramOutType);
        }

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="param_name">参数名</param>
        /// <param name="param_value">参数值</param>
        /// <param name="paramValueType">参数类型</param>
        /// <param name="paramOutType">是否是输出类型</param>
        private void Init(string param_name, object param_value, DbParamType paramValueType, bool paramOutType)
        {
            ParameterName = param_name;
            Value = param_value;
            DataType = paramValueType;
            if (paramOutType)
                Direction = ParameterDirection.Output;
            else
                Direction = ParameterDirection.Input;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns>XML</returns>
        public string ToXml()
        {
            StringBuilder sb = new StringBuilder();
            using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb))
            {
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DbParam));
                serializer.Serialize(writer, this);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="pXml">XML</param>
        /// <returns>对象</returns>
        public static DbParam FromXml(string pXml)
        {
            DbParam dp = null;
            using (System.IO.StringReader strReader = new System.IO.StringReader(pXml))
            {
                using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(strReader))
                {
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DbParam));
                    dp = (serializer.Deserialize(xmlReader) as DbParam);
                }
            }
            return dp;
        }
    }

    /// <summary>
    /// 参数集合
    /// </summary>
    public class DbParamCollection : List<DbParam>
    {
        /// <summary>
        /// 按参数名称获取参数对象
        /// </summary>
        /// <param name="parameter_name">参数名称</param>
        /// <returns>参数对象</returns>
        public DbParam this[string parameter_name]
        {
            get
            {
                DbParam item = null;
                for (int i = 0; i < base.Count; i++)
                {
                    if (base[i].ParameterName.ToUpper().Equals(parameter_name.ToUpper()))
                    {
                        item = (DbParam)base[i];
                        break;
                    }
                }
                return item;
            }
        }

        /// <summary>
        /// 获取参数名列表
        /// </summary>
        /// <returns></returns>
        public string GetParamNames()
        {
            string svalue = "";
            for (int i = 0; i < base.Count; i++)
            {
                DbParam curitem = (DbParam)base[i];
                if (curitem == null || curitem.Value == null || curitem.Value.Equals(DBNull.Value)) continue;
                svalue += ";" + curitem.ParameterName;
            }
            if (svalue.Length > 0) svalue = svalue.Substring(1);
            return svalue;
        }

        /// <summary>
        /// 获取参数值列表
        /// </summary>
        /// <returns></returns>
        public string GetParamValues()
        {
            string svalue = "";
            for (int i = 0; i < base.Count; i++)
            {
                DbParam curitem = (DbParam)base[i];
                if (curitem == null || curitem.Value == null || curitem.Value.Equals(DBNull.Value)) continue;
                svalue += ";" + curitem.Value.ToString();
            }
            if (svalue.Length > 0) svalue = svalue.Substring(1);
            return svalue;
        }

        /// <summary>
        /// 获取参数列表
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, object> GetDictionary()
        {
            Dictionary<string, object> val = new Dictionary<string, object>();
            for (int i = 0; i < base.Count; i++)
            {
                DbParam curitem = (DbParam)base[i];
                if (curitem == null || curitem.Value == null || curitem.Value.Equals(DBNull.Value)) continue;
                val.Add(curitem.ParameterName, curitem.Value);
            }
            return val;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns>XML</returns>
        public string ToXml()
        {
            StringBuilder sb = new StringBuilder();
            using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb))
            {
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DbParamCollection));
                serializer.Serialize(writer, this);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="pXml">XML</param>
        /// <returns>对象</returns>
        public static DbParamCollection FromXml(string pXml)
        {
            DbParamCollection dpc = null;
            if (pXml == null || pXml.Length == 0)
                dpc = new DbParamCollection();
            else
            {
                using (System.IO.StringReader strReader = new System.IO.StringReader(pXml))
                {
                    using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(strReader))
                    {
                        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(DbParamCollection));
                        dpc = (serializer.Deserialize(xmlReader) as DbParamCollection);
                    }
                }
            }
            return dpc;
        }
    }

    /// <summary>
    /// DbParam对象比较类
    /// </summary>
    public class DbParamCompare : IComparer<DbParam>
    {
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="x">对象1</param>
        /// <param name="y">对象2</param>
        /// <returns>差值</returns>
        public int Compare(DbParam x, DbParam y)
        {
            return (x.ParameterNo - y.ParameterNo);
        }
    }
}
